package com.snifferx.util.helper;

import java.io.File;
import java.net.URL;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.snifferx.util.properties.PropertiesManager;

public class EmailHelper implements java.io.Serializable {
	private static final String myClassName = "EmailHelper";
	public static final String exchangeJndiName = "java:comp/env/mail/ExchangeMailSession";
	private static EmailHelper _instance = null;
	private static String jndiName = null;
	private static String defaultJndiName = "java:comp/env/mail/LocalMailSession";
	private static Properties eapplnProperties;

	private EmailHelper() {

	}

	public EmailHelper getExchangeInstance() {

		_instance = new EmailHelper();
		jndiName = exchangeJndiName;
		return _instance;
	}

	private static String getJndiName() {
		String runningJndiName = defaultJndiName;

		if (jndiName != null)
			runningJndiName = jndiName;

		return runningJndiName;
	}

	private static Properties getWeblogicProperties() {
		Properties h = new Properties();
		h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.T3InitialContextFactory");
		h.put(Context.SECURITY_PRINCIPAL, "weblogic");
		h.put(Context.SECURITY_CREDENTIALS, "weblogicadmin");

		return h;
	}

	private static Properties getProperties() {
		Properties h = new Properties();
		String initialContextFactory = PropertiesManager.getEapplnPropertyAsString(PropertiesManager.JAVA_NAMING_FACTORY_INITIAL);
		String providerUrl = PropertiesManager.getEapplnPropertyAsString(PropertiesManager.JAVA_NAMING_PROVIDER_URL);
		String securityPrincipal = PropertiesManager.getEapplnPropertyAsString(PropertiesManager.JAVA_NAMING_SECURITY_PRINCIPAL);
		// String securityCredentials =
		// PropertiesManager.getEapplnPropertyAsString(PropertiesManager.JAVA_NAMING_SECURITY_CREDENTIALS);
		String securityCredentials = "Zr93436080";

		h.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
		if (providerUrl != null && !providerUrl.equals(""))
			h.put(Context.PROVIDER_URL, providerUrl);
		h.put(Context.SECURITY_PRINCIPAL, securityPrincipal);
		h.put(Context.SECURITY_CREDENTIALS, securityCredentials);

		return h;
	}

	private static Context getWeblogicInitialContext() throws NamingException {
		Properties prop = getProperties();
		Context ctx = new InitialContext(prop);
		return ctx;
	}

	public static void sendMail(String from, String recipient, String adminRecipient, String subject, String mailContent) throws Exception {
		System.out.println("from: " + from);
		System.out.println("recipient: " + recipient);
		System.out.println("adminRecipient: " + adminRecipient);

		try {
			Context initial = getWeblogicInitialContext();
			Session session = (Session) initial.lookup(getJndiName());
			System.out.println("Found mail session " + session);
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));

			if (recipient != null && !recipient.equals("")) {
				msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
			}

			if (adminRecipient != null && !adminRecipient.equals("")) {
				if (recipient != null && !recipient.equals("")) {
					msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(adminRecipient));
				} else {
					msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(adminRecipient));
				}
			}

			msg.setSubject(subject);

			DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);

			Date sysdate = new Date();
			String messageText = mailContent;
			String contentType = "text/html; charset=\"US-ASCII\"";
			msg.setText(messageText, "US-ASCII");
			msg.setSentDate(sysdate);
			Transport.send(msg);
			System.out.println("Mail sent");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void sendMail(String from, String recipient, String subject, String mailContent) throws Exception {

		try {
			Context context = getWeblogicInitialContext();
			Session session = (Session) context.lookup(getJndiName());
			System.out.println("Found mail session " + session);
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
			msg.setSubject(subject);
			DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
			Date sysdate = new Date();
			String messageText = "Email sent on " + dateFormatter.format(sysdate) + "." + "\n\n" + mailContent;
			String contentType = "text/html; charset=\"US-ASCII\"";
			msg.setText(messageText, "US-ASCII");
			msg.setSentDate(sysdate);
			Transport.send(msg);
			System.out.println("Mail sent");
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	public static void sendMailByType(String sender, String recipient, String adminRecipient, String subject, String mailContent,
			String mailContentType) throws Exception {
		String str = myClassName + ".sendMailByType(" + "sender=" + sender + ", recipient=" + recipient + ", adminRecipient=" + adminRecipient
				+ ", subject=" + subject + ", mailContentType=" + mailContentType + ")";
		System.out.println(str);
		String result = null;

		try {
			Context initial = getWeblogicInitialContext();
			Session session = (Session) initial.lookup(getJndiName());
			MimeMessage msg = new MimeMessage(session);

			msg.setFrom(new InternetAddress(sender));
			if (recipient != null && !recipient.equals("")) {
				msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
			}
			if (adminRecipient != null && !adminRecipient.equals("")) {
				if (recipient != null && !recipient.equals("")) {
					msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(adminRecipient));
				} else {
					msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(adminRecipient));
				}
			}
			msg.setSubject(subject);

			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setDisposition(Part.INLINE);
			String contentType = "text/plain";
			if (mailContentType != null) {
				if (mailContentType.trim().equalsIgnoreCase("text/html")) {
					contentType = "text/html";
				}
			}
			messageBodyPart.setContent(mailContent, contentType);
			MimeMultipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);
			msg.setContent(multipart);

			DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
			Date sysdate = new Date();
			msg.setSentDate(sysdate);

			Transport.send(msg);
			result = "Mail Sent at " + sysdate.toString();
		} catch (Exception e) {
			e.printStackTrace();
			result = e.getMessage();
		}

		System.out.println(str + ": " + result);
	}

	public static void sendSMS(String subject, String mailContent) throws Exception {
		String from = "g-svcemail@singtel.com";
		String adminRecipient = "issm@issm1";
		System.out.println("from: " + from);
		System.out.println("adminRecipient: " + adminRecipient);

		try {
			Context initial = getWeblogicInitialContext();
			Session session = (Session) initial.lookup(getJndiName());
			System.out.println("Found mail session " + session);
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(adminRecipient));
			msg.setSubject(subject);
			DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
			Date sysdate = new Date();
			String messageText = mailContent;
			String contentType = "text/html; charset=\"US-ASCII\"";
			msg.setText(messageText, "US-ASCII");
			msg.setSentDate(sysdate);
			Transport.send(msg);
			System.out.println("SMS sent");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getAdminRecipient(String msgType, String adminRecipientList) {
		StringTokenizer st = new StringTokenizer(adminRecipientList, ",");
		StringBuffer adminRecipientBuffer = new StringBuffer();
		String adminRecipient = adminRecipientList;
		String strToken;
		int i, tokenCount;

		tokenCount = st.countTokens();

		if (msgType.equals("ALL")) {
			return (adminRecipientList);
		} else {
			if (msgType.equals("EMAIL")) {
				for (i = 0; i < tokenCount; i++) {
					if (adminRecipientBuffer.length() > 0) {
						adminRecipientBuffer.append(",");
					}
					strToken = st.nextToken();
					if (strToken.indexOf("singtel") != -1 || strToken.indexOf("singnet") != -1) {
						adminRecipientBuffer.append(strToken);
					}
				}
				adminRecipient = adminRecipientBuffer.toString();
			} else {
				if (msgType.equals("SMS")) {
					for (i = 0; i < st.countTokens(); i++) {
						if (adminRecipientBuffer.length() > 0) {
							adminRecipientBuffer.append(",");
						}
						strToken = st.nextToken();
						if (strToken.indexOf("singtel") == -1 && strToken.indexOf("singnet") == -1) {
							adminRecipientBuffer.append(strToken);
						}
					}
					adminRecipient = adminRecipientBuffer.toString();
				}
			}
		}
		return adminRecipient;
	}

	public static void sendEshopAccountActivationMail(String from, String recipient, String adminRecipient, String validationCode, String custName,
			String custId, String password) throws Exception {
		System.out.println("from: " + from);
		System.out.println("recipient: " + recipient);
		System.out.println("adminRecipient: " + adminRecipient);

		try {
			String subject = "SnifferX.com account activation";

			StringBuffer messageText = new StringBuffer();
			messageText.append("Dear " + custName + ",<br/>");
			messageText.append("<br/>");
			messageText.append("Thank you for registering with SnifferX.com.<br/>");
			messageText.append("<br/>");
			messageText
					.append("Please enter the Activation PIN below on SnifferX.com within 90 minutes to complete your registration process.<br/>");
			messageText.append("<br/>");
			messageText.append("<b><u>Activation PIN: " + validationCode + "</u></b><br/>");
			messageText.append("<br>");
			messageText.append("All future correspondences from SnifferX.com will be sent to your registered email address.<br/>");
			messageText.append("<br/>");
			messageText.append("If you have not registered for a SnifferX.com account, please ignore this email.<br/>");
			messageText.append("<br/>");
			messageText.append("<br/>");
			messageText.append("This is a computer-generated email, please do not reply.<br/>");
			messageText.append("<br/>");
			messageText.append("Thank you,<br/>");
			messageText.append("SnifferX.com team<br/>");

			sendHTMLStringMail(from, new String[] { recipient }, null, new String[] { adminRecipient }, subject, messageText.toString());

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}

	public static void sendEshopShoppingActivationMail(String from, String recipient, String adminRecipient, String validationCode, String custName)
			throws Exception {
		System.out.println("from: " + from);
		System.out.println("recipient: " + recipient);
		System.out.println("adminRecipient: " + adminRecipient);
		try {
			Context initial = getWeblogicInitialContext();
			Session session = (Session) initial.lookup(getJndiName());
			System.out.println("Found mail session " + session);
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(adminRecipient));
			msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
			msg.setSubject("SingTel eShop Activation");
			DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
			Date sysdate = new Date();
			StringBuffer messageText = new StringBuffer();
			messageText.append("Dear " + custName + ",\n");
			messageText.append("\n");
			messageText.append("Please enter the Activation PIN below on the eShop website within 30 mins to continue with your shopping.\n");
			messageText.append("\n");
			messageText.append("Activation PIN: " + validationCode + "\n");
			messageText.append("\n");
			messageText.append("Any future notifications and alerts from SingTel eShop will be sent to this email address.\n");
			messageText.append("\n");
			messageText.append("Thank you for choosing SingTel as your communications partner.\n");
			messageText.append("\n");
			messageText.append("If you did not register for the SingTel eShop service, please ignore this email.\n");
			messageText.append("\n");
			messageText.append("This is a computer-generated email, please do not reply.\n");
			String contentType = "text/html; charset=\"US-ASCII\"";
			msg.setText(messageText.toString(), "US-ASCII");
			msg.setSentDate(sysdate);
			Transport.send(msg);
			System.out.println("Mail sent");
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}

	public static void sendEshopHTMLMail(String sender, String recipient, String adminRecipient, String subject, String htmlUrl) throws Exception {
		try {
			System.out.println("sendEshopHTMLMail");
			Session session = null;
			try {
				Context initial = getWeblogicInitialContext();
				session = (Session) initial.lookup(getJndiName());
				System.out.println("Found mail session " + session);
			} catch (Exception ex) {
				System.out.println("-----------Start of Exception:" + ex.getMessage() + "--------------");
				ex.printStackTrace();
				System.out.println("-----------End of Exception:" + ex.getMessage() + "--------------");
			}

			MimeMessage msg = new MimeMessage(session);
			msg.setSubject(subject);
			msg.addFrom(InternetAddress.parse(sender));
			msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(recipient));
			if (adminRecipient != null && !adminRecipient.equals(""))
				msg.setRecipients(javax.mail.Message.RecipientType.BCC, InternetAddress.parse(adminRecipient));
			BodyPart messageBodyPart = new MimeBodyPart();
			URL url = new URL(htmlUrl);
			messageBodyPart.setDataHandler(new DataHandler(url));
			MimeMultipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);
			msg.setContent(multipart);
			Transport.send(msg);

			System.out.println("Mail sent");
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
	}

	public static void sendEshopHTMLMailWithPDFAttachment(String sender, String recipient, String adminRecipient, String subject, String htmlUrl,
			String[] pdfURL, String[] pdfName) throws Exception {

		try {

			Context initial = getWeblogicInitialContext();
			Session session = (Session) initial.lookup(getJndiName());
			System.out.println((new StringBuilder()).append("Found mail session ").append(session).toString());
			MimeMessage msg = new MimeMessage(session);
			msg.setSubject(subject);
			msg.addFrom(InternetAddress.parse(sender));
			msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(recipient));
			if (adminRecipient != null && !adminRecipient.equals(""))
				msg.setRecipients(javax.mail.Message.RecipientType.BCC, InternetAddress.parse(adminRecipient));
			BodyPart messageBodyPart = new MimeBodyPart();
			URL url = new URL(htmlUrl);
			messageBodyPart.setDataHandler(new DataHandler(url));
			MimeMultipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);
			if ((pdfURL != null) && (pdfName != null)) {
				for (byte i = 0; i < pdfURL.length; i++) {
					messageBodyPart = new MimeBodyPart();
					System.out.println("pdfURL:" + pdfURL[i]);
					System.out.println("pdfName:" + pdfName[i]);
					URL pdf = new URL(pdfURL[i]);
					messageBodyPart.setDataHandler(new DataHandler(pdf));
					messageBodyPart.setFileName(pdfName[i]);
					multipart.addBodyPart(messageBodyPart);
				}
			}
			msg.setContent(multipart);
			Transport.send(msg);
			System.out.println("Mail sent");
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}

	}

	public static void sendEshopMailWithFileAttachment(String sender, String recipient, String adminRecipient, String subject, String filename,
			FileDataSource fds, String subjectBodyPartText) throws Exception {
		try {
			Context initial = getWeblogicInitialContext();
			Session session = (Session) initial.lookup("java:comp/env/mail/SingTelEmailEJB");
			System.out.println((new StringBuilder()).append("Found mail session ").append(session).toString());
			MimeMessage msg = new MimeMessage(session);
			msg.setSubject(subject);
			msg.addFrom(InternetAddress.parse(sender));
			msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(recipient));
			msg.setRecipients(javax.mail.Message.RecipientType.BCC, InternetAddress.parse(adminRecipient));
			Multipart multipart = new MimeMultipart();
			BodyPart messageBodyPart = new MimeBodyPart();
			BodyPart subjectBodyPart = new MimeBodyPart();
			subjectBodyPart.setText(subjectBodyPartText);
			multipart.addBodyPart(subjectBodyPart);
			messageBodyPart.setDataHandler(new DataHandler(fds));
			messageBodyPart.setFileName(filename);
			multipart.addBodyPart(messageBodyPart);
			msg.setContent(multipart);
			Transport.send(msg);
			System.out.println("Mail sent");
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}

	public static void sendHTMLStringMail(String from, String recipient, String subject, String mailContent) throws Exception {

		try {
			Context initial = getWeblogicInitialContext();
			Session session = (Session) initial.lookup(getJndiName());
			System.out.println("Found mail session " + session);
			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));
			msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
			msg.setSubject(subject);
			DateFormat dateFormatter = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.SHORT);
			Date sysdate = new Date();
			String htmlText = mailContent;
			Multipart multipart = new MimeMultipart();
			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setContent(htmlText, "text/html");
			multipart.addBodyPart(messageBodyPart);
			msg.setContent(multipart);
			String messageText = mailContent;
			String contentType = "text/html;";
			Transport.send(msg);
			System.out.println("Mail sent");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void sendEshopAdminHTMLEmailWithHTMLAttachment(String creationUserId, String subject, List<String> toList, String senderEmail,
			String htmlUrl, String htmlAttachmentURL, String htmlAttachmentFileName, List<String> bccList) throws Exception {
		ArrayList<File> filesList = new ArrayList<File>();
		System.out.println("EmailHelper:sendAdminHTMLEmailWithAttachment(" + creationUserId + "," + subject + "," + toList + "," + htmlUrl + ","
				+ bccList);
		try {
			String sender = creationUserId + " <" + senderEmail + ">";
			Context initial = new InitialContext();
			System.out.println(jndiName);
			Session session = (Session) initial.lookup(getJndiName());
			System.out.println("Found mail session " + session);
			MimeMessage msg = new MimeMessage(session);
			msg.setSubject(subject);
			msg.addFrom(InternetAddress.parse(sender));
			if (toList != null) {
				for (String toEmailAddr : toList) {
					msg.addRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(toEmailAddr));

				}
			}
			if (bccList != null) {
				for (String bccEmailAddr : bccList) {
					msg.addRecipients(javax.mail.Message.RecipientType.BCC, InternetAddress.parse(bccEmailAddr));
				}
			}
			BodyPart messageBodyPart = new MimeBodyPart();
			URL url = new URL(htmlUrl);
			messageBodyPart.setDataHandler(new DataHandler(url));
			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(messageBodyPart);
			if (htmlAttachmentURL != null && !htmlAttachmentURL.equals("")) {
				BodyPart attachmentBodyPart = new MimeBodyPart();
				URL urlAttachment = new URL(htmlAttachmentURL);
				attachmentBodyPart.setDataHandler(new DataHandler(urlAttachment));
				String filename = htmlAttachmentFileName + ".html";
				attachmentBodyPart.setFileName(filename);
				multipart.addBodyPart(attachmentBodyPart);
			} else {
				System.out.println("HTML page attachment not required.");
			}

			msg.setContent(multipart);
			Transport.send(msg);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		} finally {
			System.out.println("At Finally: FileList Size" + filesList.size());
			if (filesList != null && filesList.size() > 0) {
				for (int i = 0; i < filesList.size(); i++) {
					File oFile = filesList.get(i);
					if (oFile != null && oFile.exists()) {
						System.out.println("oFile Exist!");
						oFile.delete();
					}
				}
			}
		}
	}

	public static void sendEshopMailWithFileAttachments(String sender, String recipient, String adminRecipient, String subject,
			String subjectBodyPartText, ArrayList<String> filenames, ArrayList<DataSource> fds) throws Exception {
		try {

			Context initial = getWeblogicInitialContext();

			// Session session = (Session)
			// initial.lookup("java:comp/env/mail/SingTelEmailEJB");
			Session session = (Session) initial.lookup(getJndiName());

			System.out.println((new StringBuilder()).append("Found mail session ").append(session).toString());

			MimeMessage msg = new MimeMessage(session);

			msg.setSubject(subject);

			msg.addFrom(InternetAddress.parse(sender));

			msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(recipient));

			if (adminRecipient != null) {
				msg.setRecipients(javax.mail.Message.RecipientType.BCC, InternetAddress.parse(adminRecipient));
			}

			// Set Subject
			Multipart multipart = new MimeMultipart();
			BodyPart messageBodyPart = new MimeBodyPart();
			BodyPart subjectBodyPart = new MimeBodyPart();
			subjectBodyPart.setText(subjectBodyPartText);
			multipart.addBodyPart(subjectBodyPart);

			/*
			 * FileDataSource fds = new FileDataSource(fileName);
			 * messageBodyPart.setDataHandler(new DataHandler(fds));
			 * messageBodyPart.setFileName(filename);
			 * multipart.addBodyPart(messageBodyPart);
			 */

			if ((fds != null) && (filenames != null)) {
				for (byte i = 0; i < fds.size(); i++) {
					messageBodyPart = new MimeBodyPart();

					System.out.println("fds:" + fds.get(i));
					System.out.println("filenames:" + filenames.get(i));

					messageBodyPart.setDataHandler(new DataHandler(fds.get(i)));
					messageBodyPart.setFileName(filenames.get(i));
					multipart.addBodyPart(messageBodyPart);
				}
			}

			msg.setContent(multipart);

			Transport.send(msg);

			System.out.println("Mail sent");
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());
		}
	}

	// added by Maria on Dec 28, 2010 for mio tv send email pdf
	// updated on feb 11, 2011 to accommodate more than 1 attachment.
	public static void sendEshopHTMLMailWithPDFAttachment(String sender, String recipient, String adminRecipient, String subject,
			String htmlBodyMessage, ArrayList<String> pdfURL, ArrayList<String> pdfName) throws Exception {

		try {
			// Properties h = getWeblogicProperties();

			Context initial = getWeblogicInitialContext();

			// Session session = (Session)
			// initial.lookup("java:comp/env/mail/SingTelEmailEJB");

			Session session = (Session) initial.lookup(getJndiName());

			System.out.println((new StringBuilder()).append("Found mail session ").append(session).toString());

			MimeMessage msg = new MimeMessage(session);

			msg.setSubject(subject);

			msg.addFrom(InternetAddress.parse(sender));

			msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(recipient));

			msg.setRecipients(javax.mail.Message.RecipientType.BCC, InternetAddress.parse(adminRecipient));

			BodyPart messageBodyPart = new MimeBodyPart();

			// URL url = new URL(htmlUrl);

			// messageBodyPart.setDataHandler(new DataHandler(url));
			messageBodyPart.setContent(htmlBodyMessage, "text/html");

			MimeMultipart multipart = new MimeMultipart();

			multipart.addBodyPart(messageBodyPart);

			if ((pdfURL != null) && (pdfName != null)) {

				for (byte i = 0; i < pdfURL.size(); i++) {

					messageBodyPart = new MimeBodyPart();

					System.out.println("pdfURL:" + pdfURL.get(i));

					System.out.println("pdfName:" + pdfName.get(i));

					URL pdf = new URL(pdfURL.get(i));

					messageBodyPart.setDataHandler(new DataHandler(pdf));

					messageBodyPart.setFileName(pdfName.get(i));

					multipart.addBodyPart(messageBodyPart);

				}

			}

			msg.setContent(multipart);

			Transport.send(msg);

			System.out.println("Mail sent");

		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e.getMessage());

		}

	}

	public static void sendHTMLStringMail(String from, String[] tos, String subject, String mailContent) throws Exception {
		sendHTMLStringMail(from, tos, null, null, subject, mailContent);
	}

	public static void sendHTMLStringMail(String from, String[] tos, String[] ccs, String subject, String mailContent) throws Exception {
		sendHTMLStringMail(from, tos, ccs, null, subject, mailContent);
	}

	public static void sendHTMLStringMail(String from, String[] tos, String[] ccs, String[] bccs, String subject, String mailContent)
			throws Exception {
		try {
			Context initial = getWeblogicInitialContext();
			Session session = (Session) initial.lookup(getJndiName());

			System.out.println("Found mail session " + session);

			MimeMessage msg = new MimeMessage(session);
			msg.setFrom(new InternetAddress(from));

			if (null != tos) {
				Address[] toList = new InternetAddress[tos.length];

				for (int i = 0; i < tos.length; i++) {
					Address item = new InternetAddress(tos[i]);
					toList[i] = item;
				}

				msg.setRecipients(Message.RecipientType.TO, toList);
			}

			if (null != ccs) {
				Address[] ccsList = new InternetAddress[ccs.length];

				for (int i = 0; i < ccs.length; i++) {
					Address item = new InternetAddress(ccs[i]);
					ccsList[i] = item;
				}

				msg.setRecipients(Message.RecipientType.CC, ccsList);
			}

			if (null != bccs) {
				Address[] bccsList = new InternetAddress[bccs.length];

				for (int i = 0; i < bccs.length; i++) {
					Address item = new InternetAddress(bccs[i]);
					bccsList[i] = item;
				}

				msg.setRecipients(Message.RecipientType.BCC, bccsList);
			}

			msg.setSubject(subject);

			String htmlText = mailContent;
			Multipart multipart = new MimeMultipart();
			BodyPart messageBodyPart = new MimeBodyPart();
			messageBodyPart.setContent(htmlText, "text/html");
			multipart.addBodyPart(messageBodyPart);

			msg.setContent(multipart);

			Transport.send(msg);

			System.out.println("Mail sent");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
